package server.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.pojo.*;
import server.business.service.*;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.JsonResult;
import server.business.utils.results.lrz.Result;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;
    @Autowired
    private NationalityService nationalityService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private AboTypeService aboTypeService;
    @Autowired
    private RhTypeService rhTypeService;

    /**
     * 添加患者信息
     *
     * @param patient
     * @return
     */

    @RequestMapping(value = "/normal/addpatient", method = RequestMethod.POST)
    public JsonResult<Object> patientAdd(@RequestBody @Valid Patient patient) {

        Long medicalRecordNumberId = Long.valueOf(patient.getNationality() + patient.getBedNumber() + patient.getAge());
        GenerateNumberBySnowFlake medicalRecordNumber = new GenerateNumberBySnowFlake(medicalRecordNumberId, "Med");
        patient.setMedicalRecordNumber(medicalRecordNumber.genNumberByflag());
        System.out.println(medicalRecordNumber.genNumberByflag());
        int isStatus = patientService.patientAdd(patient);

        if (isStatus == 0) {
            return JsonResult.error("患者信息录入失败");
        }
        return JsonResult.success();

    }

    /**
     * 获取民族数据字典
     *
     * @return
     */
    @GetMapping("/normal/nationality")
    public List<Nationality> getNationalityInfo() {
        return nationalityService.list();
    }

    /**
     * 获取ABO血型数据字典
     */
    @GetMapping("/normal/aboType")
    public List<AboType> getAboType() {
        return aboTypeService.list();
    }

    /**
     * 获取Rh血型数据字典
     */
    @GetMapping("/normal/rhType")
    public List<RhType> getRhType() {
        return rhTypeService.list();
    }

    /**
     * 获取科室数据字典
     *
     * @return
     */
    @RequestMapping(value = "/normal/department", method = RequestMethod.GET)
    @ResponseBody
    public List<Department> getDepartmentInfo(HttpServletRequest httpServletRequest) {
        // System.out.println(httpServletRequest.getParameter("code1"));
        String code1 = httpServletRequest.getParameter("code1");
        List<Department> departments = departmentService.findChildByCode(code1);
        return departments;
    }

    /**
     * 查询所有一级科室
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/normal/department_list", method = RequestMethod.GET)
    @ResponseBody
    public List<Department> selectDepartmentByCode(String code) {

        List<Department> departments = departmentService.selectDepartmentByCode(code);
        // System.out.println(departments);
        return departments;
    }

    /**
     * 获取所有患者信息
     *
     * @return
     */

    @RequestMapping("/normal/list")
    public JsonResult<Object> listPage() {
        Page<Patient> pageList = patientService.getPatientPage();
        return JsonResult.success(pageList);
    }

    /**
     * 修改患者信息
     *
     * @param patient
     * @return
     */
    @PutMapping(value = "/normal/update")
    public boolean save(@RequestBody Patient patient) {
        return patientService.updateById(patient);
    }

    /**
     * 分页查询
     */
    @GetMapping("/normal/{currentPage}/{pageSize}")
    public JsonResult<Object> getPage(@PathVariable int currentPage, @PathVariable int pageSize, Patient patient) {
        IPage<Patient> page = patientService.getPage(currentPage, pageSize, patient);
        if (currentPage > page.getPages()) {
            page = patientService.getPage((int) page.getPages(), pageSize, patient);
        }
        return JsonResult.success(page);
    }

    /**
     * 批量删除
     */
    @RequestMapping("/normal/batchDelete")
    @ResponseBody
    public JsonResult<Object> batchDelete(@RequestBody ArrayList<Integer> ids) {
        System.out.println(ids);
        boolean success = patientService.removeByIds(ids);
        if (success) {
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }

    // *************************************lrz*************************start
    @GetMapping("/normal/getIdcardById")
    @ResponseBody
    public server.business.utils.results.lrz.Result getIdcardById(Long Id) {
        String Idcard = patientService.getIdcardById(Id);
        server.business.utils.results.lrz.Result result = new server.business.utils.results.lrz.Result();
        if (Idcard == null) {
            result.setData(null);
            result.setMsg("未找到病人身份信息");
            result.setCode("200");
            return result;
        }
        return server.business.utils.results.lrz.Result.success(Idcard);
    }

    /**
     * 根据患者身份证号获取患者ID
     *
     * @param
     * @return
     */
    @GetMapping(value = "/normal/getPatientId")
    @ResponseBody

    public Result getPatientId(String patientIdcard) {
        Long patientId = patientService.getIdByIdcard(patientIdcard);
        return server.business.utils.results.lrz.Result.success(patientId);
    }
    // *************************************lrz*************************end

}
